package com.hqd.jjvm.classformat.attributeinfo;

import com.hqd.jjvm.classformat.ClassFile;
import com.hqd.jjvm.util.HexStrTransformUtil;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor
public class RuntimeVisibleAnnotations extends AnnotationAttributeInfo {
    private Integer numOfAnnotations;
    private List<ElementValuePair.Annotation> annotationList;

    public RuntimeVisibleAnnotations(Integer attributeNameIndex, Integer attributeLength, Integer numOfAnnotations) {
        super(attributeNameIndex, attributeLength);
        this.numOfAnnotations = numOfAnnotations;
    }

    public RuntimeVisibleAnnotations(Integer attributeNameIndex, Integer attributeLength, Integer numOfAnnotations, List<ElementValuePair.Annotation> annotationList) {
        super(attributeNameIndex, attributeLength);
        this.numOfAnnotations = numOfAnnotations;
        this.annotationList = annotationList;
    }

    @Override
    protected RuntimeVisibleAnnotations newAttributeInfo(Integer attributeNameIndex, Integer attributeLength, ClassFile classFile) {
        this.numOfAnnotations = HexStrTransformUtil.parseHexToInt(classFile.readU2());
        String code = classFile.read(attributeLength - 2, true);
        this.setByteCodes(code.getBytes());
        this.annotationList = parseAnnotation(classFile, numOfAnnotations);
        return this;
    }
}
